package com.longrise.android.byjk.plugins.course.mycourse;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.AllCourseChildBean;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.PrintLog;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by callxkj on 2017/10/25.
 * 我的课程
 */

public class MyCourseDetailPresenter extends MyCourseDetailContract.Presenter {

    private static final String TAG = "MyCourseDetailPresenter";
    private List<AllCourseChildBean> mChildBeanList = new ArrayList<>();

    @Override
    protected void init() {

    }

    private void getDataFromNet(String traintype){
        String usersfzh = UserInfor.getInstance().getUsersfzh();
        EntityBean bean = new EntityBean();
        bean.set("traintype", traintype);
        bean.set("cardno", usersfzh);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        PrintLog.d(TAG, "fbean:" + fbean);
        LoadDataManager.getInstance().callService("", UrlConstants.BaseUrl,
                "course_sMyCourseBeansByClassify", fbean, new LoadDataManager.OnRequestCallListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        parseAllTrainData(result);
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onFinished(String key, String service) {
                        mView.setRefreshing(false);
                    }
                });
    }


    private void parseAllTrainData(Object result) {
        try {
            if (result != null) {
                EntityBean bean = (EntityBean) result;
                PrintLog.d(TAG, "BEAN22:" + bean);
                int resultstate = bean.getInt("resultstate");
                if (resultstate == 1) {
                    mChildBeanList.clear();
                    EntityBean[] results = bean.getBeans("result");
                    for (EntityBean resultBean : results) {
                        AllCourseChildBean childBean = new AllCourseChildBean();
                        childBean.setRealpic(resultBean.getString("realpic",""));
                        childBean.setName(resultBean.getString("name",""));
                        childBean.setExpiretime(resultBean.getString("expiretime",""));
                        childBean.setIspass(resultBean.getBoolean("ispass",false));
                        childBean.setCourseid(resultBean.getString("groupid",""));
                        mChildBeanList.add(childBean);
                    }
                    mView.refreshData(mChildBeanList);
                }else{
                    mView.showToast(bean.getString("resultdesc"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    void getData(String traintype) {
        mView.setRefreshing(true);
        getDataFromNet(traintype);
    }
}
